package com.tadpole.music.view.fragment.study;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.study.ExamsAdapter;
import com.tadpole.music.bean.study.MyExamsFootBean;
import com.tadpole.music.bean.study.MyExamsHeadBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.study.MyExamsIView;
import com.tadpole.music.presenter.study.MyExamsPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.study.ErrorExamActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;

import java.util.List;

/**
 * 我的考试
 * Created By 孙岩
 * on 2020/9/28
 */
public class MyExamsFragment extends BaseFragment implements MyExamsIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    //college_id
    private String mParam1;
    //name
    private String mParam2;
    //desc
    private String mParam3;
    //最高分数
    private TextView tvScore;
    //累计考试次数
    private TextView tvName;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //recyclerView
    private RecyclerView recyclerView;
    //adapter
    private ExamsAdapter examsAdapter;
    //desc
    private TextView tvDesc;
    //presenter
    private MyExamsPresenter myExamsPresenter;

    public static MyExamsFragment newInstance(String param1, String param2,String param3) {
        MyExamsFragment fragment = new MyExamsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_my_exams;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(View mRootView) {
        tvScore = mRootView.findViewById(R.id.tvScore);
        tvName = mRootView.findViewById(R.id.tvName);
        rl_refresh = mRootView.findViewById(R.id.rl_refresh);
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        tvDesc = mRootView.findViewById(R.id.tvDesc);

        myExamsPresenter = new MyExamsPresenter();
        myExamsPresenter.attachView(this);
        myExamsPresenter.getMyExamsHead(mParam1);

        myExamsPresenter.getExamsFoot(mParam1,false);

        tvDesc.setText("说明:"+mParam2+mParam3);

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                myExamsPresenter.getExamsFoot(mParam1,true);
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                myExamsPresenter.getExamsFootMore(mParam1);
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showExamsHead(MyExamsHeadBean.DataBean bean) {
        tvScore.setText(bean.getMax()+"");
        tvName.setText("累计考试："+bean.getTotal()+"次");
    }

    @Override
    public void showExamsFoot(final List<MyExamsFootBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        examsAdapter = new ExamsAdapter(getActivity(),list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(examsAdapter);

        examsAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    Intent intent = new Intent(getActivity(), ErrorExamActivity.class);
                    intent.putExtra("id",list.get(position).getId());
                    intent.putExtra("flag","1");
                    startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });
    }

    @Override
    public void notifyAdapter() {
        examsAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }

    @Override
    public void show401() {
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
}
